const { getAllArtArtist } = require("../../../utils/request");
const config = require("../../../utils/config");
Page({
  data: {
    artists: [], // 用于存储艺术家数据的数组
    pageNum: 1, // 当前页数
    pageSize: 10, // 每页显示的条数
    isLoading: false // 新增加载状态
  },
  onLoad() {
    this.loadArtists();
  },
  async loadArtists() {
    if (this.data.isLoading) return; // 防止重复加载
    this.setData({ isLoading: true });
  
    try {
      let { pageNum, pageSize, artists } = this.data;
      const response = await getAllArtArtist({ pageNum, pageSize });
  
      if (!response || !response.data) {
        console.error("获取艺术家数据失败");
        return;
      }
  
      const newArtists = response.data.map(artist => ({
        ...artist,
        photoUrl: `${config.ImageURL}${artist.photoUrl}`  // 拼接完整的图片 URL
      }));
  
      this.setData({ artists: [...artists, ...newArtists] });
    } catch (error) {
      console.error("加载艺术家信息数据失败:", error);
    } finally {
      this.setData({ isLoading: false }); // 结束加载状态
    }
  },
  //向下滑动增加更多的数据
  onReachBottom() {
    const newPageNum = this.data.pageNum + 1; // 页码加1
    console.log("加载下一页数据");
    this.setData({ pageNum: newPageNum }, () => {
      this.loadArtists();
    });
  },  
  goToArtistDetail(e) {
    const id = e.currentTarget.dataset.id;
    console.log("跳转到艺术家详细页面，id：" + id);
    wx.navigateTo({
      url: `/pages/artist/detail/detail?id=${id}`
    });
  }
});
